/*
 * ReceiverDevice.java
 *
 * Created on 17 marzo 2007, 21.46
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package com.tracker.request;

import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;

import com.tracker.bean.LoggerPoints;
import com.tracker.impl.LoggerPointsImpl;

public class ReceiverDevice {
	private static Logger logger = Logger.getLogger("ReceiverDevice");

    /** Creates a new instance of ReceiverDevice */
    public ReceiverDevice(HttpServletRequest request) {
        logger.debug("Start ReceiverDevice");

        try{
		        List<LoggerPoints> lsCoord = new ArrayList<LoggerPoints>();
		        LoggerPointsImpl loggerPointsImpl = new LoggerPointsImpl();

		        if(request.getParameter("mode").equals("tracker") && request.getParameter("imei")!=null) {
		        	loggerPointsImpl.save(getParamGPSTracker(request));
		        }

        }catch(Exception ex){
        	ex.printStackTrace();
            logger.error(ex.getMessage());
        }

    }

   public LoggerPoints getParamGPSTracker(HttpServletRequest request){
        LoggerPoints loggerPoints = new LoggerPoints();
        String rmc = "";
        if(request.getParameter("imei")!=null){
        	loggerPoints.setImei(request.getParameter("imei"));
            logger.debug("IMEI:  " + request.getParameter("imei"));
        }
        if(request.getParameter("rmc")!=null){
            rmc = request.getParameter("rmc");
        }
       logger.debug("RMC = " + rmc);
       FilterGPRMCTracker(rmc, loggerPoints);
        
        return loggerPoints;
    }
    
    public void FilterGPRMCTracker(String rmc, LoggerPoints loggerPoints){
        rmc = rmc.replaceAll(",",";");
        String typeService = "";        
        String time = "";
        String date = "";

        StringTokenizer tokenizer = new StringTokenizer(rmc, ";");
        tokenizer.nextToken();
        time = (tokenizer.nextToken().substring(0,6));
        loggerPoints.setTime(time.substring(0,2)+":"+time.substring(2,4)+":"+time.substring(4,6)); 
        
        tokenizer.nextToken();
        loggerPoints.setLatitude(tokenizer.nextToken());
        loggerPoints.setLatitudeDir(tokenizer.nextToken());
        loggerPoints.setLongitude(tokenizer.nextToken());
        loggerPoints.setLongitudeDir(tokenizer.nextToken());
        
        String speed = tokenizer.nextToken();
        speed = ((speed.equals(""))||(speed == null)) ? "0" : speed;
        
        loggerPoints.setSpeed(speed);
        
        tokenizer.nextToken();  //courseMadeGood
        //loggerPoints.setCourseMadeGood(Double.parseDouble(tokenizer.nextToken()));

        date = tokenizer.nextToken();
        loggerPoints.setDate(date.substring(0,2)+"/"+date.substring(2,4)+"/20"+date.substring(4,6));
        tokenizer.nextToken();
        typeService = tokenizer.nextToken();
        loggerPoints.setTypeService((typeService.equals("0")) ? "AUTO":"SOS");
          
    } 
    
/*
RMC - NMEA has its own version of essential gps pvt (position, velocity, time) data. It is called RMC, The Recommended Minimum, which will look similar to:

$GPRMC,123519,A,4807.038,N,01131.000,E,022.4,084.4,230394,003.1,W*6A

Where:
     RMC          Recommended Minimum sentence C
     123519       Fix taken at 12:35:19 UTC
     A            Status A=active or V=Void.
     4807.038,N   Latitude 48 deg 07.038' N
     01131.000,E  Longitude 11 deg 31.000' E
     022.4        Speed over the ground in knots
     084.4        Track angle in degrees True
     230394       Date - 23rd of March 1994
     003.1,W      Magnetic Variation
     *6A          The checksum data, always begins with *
     
*/
    
}
